package com.hxm.appGenealogy.controller;

import com.alibaba.fastjson.JSON;
import com.hxm.appGenealogy.Function.PeopleFunction;
import com.hxm.appGenealogy.config.ParmIntended;
import com.hxm.appGenealogy.config.queryVo.QueryWrapperVO;
import com.hxm.appGenealogy.entity.Family;
import com.hxm.appGenealogy.entity.People;
import com.hxm.appGenealogy.entity.PeopleVo;
import com.hxm.appGenealogy.entity.ResponDataVo;
import com.hxm.appGenealogy.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@CrossOrigin
@RestController
@RequestMapping("app/people")
public class UserController {

    @Autowired
    private UserService userService;


    @Autowired
    private PeopleFunction peopleFunction;

    @PostMapping("addPeople")
    public ResponDataVo addPeople(@RequestBody People people) {
        peopleFunction.insertPeople(people);
        return new ResponDataVo(true);
    }

    //获取当个用户;
    @PostMapping("getByid")
    public ResponDataVo getUser(@RequestBody People people) {
        return userService.getUser(people);
    }

    //修改用户的信息;
    @PostMapping("updateByid")
    public ResponDataVo updateByid(@RequestBody People people) {
        /*People people1 = peopleFunction.peopleGetByid(people.getId());
        if (people1.getNumber() != null) {
            return new ResponDataVo("当前的手机号已经存在,不可以在次的进行绑定;");
        }*/
        return userService.updateByid(people);
    }

    /**
     * 查询申请申请家族中的人;
     *
     * @param family
     * @return
     */
    @PostMapping("selectJoin")
    public ResponDataVo selectJoin(@RequestBody Family family) {
        String parmAnalysis = ParmIntended.parmAnalysis(family, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        List<People> peopleList = peopleFunction.queryGeListString(JSON.toJSONString(new QueryWrapperVO().eq("family_id", family.getId())));
        List<People> peopleListJoin = new ArrayList<>();
        peopleList.forEach(t -> {
            if (t.getIsHavePit() == 3) {
                peopleListJoin.add(t);
            }
        });
        return new ResponDataVo(peopleListJoin);
    }

    /**
     * 迁移家族后重新登录;
     *
     * @param people
     * @return
     */
    @PostMapping("selectisjoin")
    public ResponDataVo isJoin(@RequestBody People people) {
        String parmAnalysis = ParmIntended.parmAnalysis(people, "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        if (people.getFamilyMergeId() != null) {
            return new ResponDataVo("您的家族已经进行了迁移请重新登录");
        }
        return new ResponDataVo(true);
    }

    /**
     * 删除人的
     */
    @PostMapping("delPeople")
    public ResponDataVo delPeople(@RequestBody PeopleVo peopleVo) {
        String parmAnalysis = ParmIntended.parmAnalysis(peopleVo, "myPeopleId", "id");
        if (parmAnalysis != null) {
            return new ResponDataVo(parmAnalysis);
        }
        //全新信息的判断;
        People peopleLogin = peopleFunction.peopleGetByid(peopleVo.getMyPeopleId());
        if (peopleLogin == null) {
            return new ResponDataVo("请进行登录;");
        }
        if (peopleLogin.getJurisdiction() == 2) {
            return new ResponDataVo("您没有权限来进行当前的移除未分配人员的信息的操作;");
        }
        People delPeople = peopleFunction.peopleGetByid(peopleVo.getId());
        if (delPeople == null) {
            return new ResponDataVo("没有当前的人的信息;");
        }
        //判断他是不是迁移的家族的人员的信息;
        if (delPeople.getFamilyMergeId() == null) {
            return new ResponDataVo("当前的人员信息不是未分配的人员的信息;");
        }
        peopleFunction.delPeopleByid(delPeople.getId());
        return new ResponDataVo(true);
    }

}




